package org.dbstar.scheduler.beans;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.dbstar.scheduler.identifier.Identifier;


/**
 * 符合Job的子Job定义
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-5-6
 */
public class JobDefine implements Serializable, Cloneable {
	private static final long serialVersionUID = -8141732352749398694L;

	private Identifier identifier;
	private Map<String, String> parameters;
	private Set<Integer> dependencies;
	private String condition;
	private boolean skip;

	public JobDefine(Identifier identifier, Map<String, String> parameters, Set<Integer> dependencies,
			String condition, boolean skip) {
		super();
		setIdentifier(identifier);
		setParameters(parameters);
		setDependencies(dependencies);
		setCondition(condition);
		this.skip = skip;
	}

	public Identifier getIdentifier() {
		return identifier;
	}

	private void setIdentifier(Identifier identifier) {
		this.identifier = identifier;
	}

	public Map<String, String> getParameters() {
		return parameters == null || parameters.isEmpty() ? null : Collections.unmodifiableMap(parameters);
	}

	private void setParameters(Map<String, String> parameters) {
		this.parameters = parameters == null || parameters.isEmpty() ? null : parameters;
	}

	public Set<Integer> getDependencies() {
		return dependencies == null || dependencies.isEmpty() ? null : Collections.unmodifiableSet(dependencies);
	}

	private void setDependencies(Set<Integer> dependencies) {
		this.dependencies = dependencies == null || dependencies.isEmpty() ? null : dependencies;
	}

	public String getCondition() {
		return condition;
	}

	private void setCondition(String condition) {
		this.condition = StringUtils.isBlank(condition) ? null : condition;
	}

	public boolean isSkip() {
		return skip;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
		result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		JobDefine other = (JobDefine) obj;
		if (identifier == null) {
			if (other.identifier != null)
				return false;
		} else if (!identifier.equals(other.identifier))
			return false;
		if (parameters == null) {
			if (other.parameters != null)
				return false;
		} else if (!parameters.equals(other.parameters))
			return false;
		return true;
	}

	@Override
	public JobDefine clone() throws CloneNotSupportedException {
		JobDefine jd = (JobDefine) super.clone();
		if (parameters != null) {
			jd.parameters = new HashMap<String, String>(parameters);
		}
		if (dependencies != null) {
			jd.dependencies = new HashSet<Integer>(dependencies);
		}
		return jd;
	}
}
